﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Users_PassengerDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["DepartTicketList"] == null || Session["DepartRequestType"] == null)
        {
            Response.Redirect("~/SearchFlight3.aspx");
        }

        int adult = Convert.ToInt32(Session["Adult"]);
        int child = Convert.ToInt32(Session["Child"]);
        int senior = Convert.ToInt32(Session["Senior"]);
        int x = 0;
        for (int i = 0; i < adult; i++)
        {
            CustomTable table = new CustomTable(x.ToString(), "Adult");
            PlaceHolder1.Controls.Add(table);
            x++;
        }

        for (int i = 0; i < child; i++)
        {
            CustomTable table = new CustomTable(x.ToString(), "Children");
            PlaceHolder1.Controls.Add(table);
            x++;
        }

        for (int i = 0; i < senior; i++)
        {
            CustomTable table = new CustomTable(x.ToString(), "Senior");
            PlaceHolder1.Controls.Add(table);
            x++;
        }
    }

    private class CustomTable : Table
    {
        public CustomTable(string _id, string passengerType)
        {
            this.Width = Unit.Percentage(100);
            this.ID = "table" + _id;

            #region Row 1 passenger type
            TableRow row1 = new TableRow();
            row1.BackColor = System.Drawing.Color.AntiqueWhite;
            row1.Font.Size = FontUnit.Large;
            row1.Font.Bold = true;
            TableCell cel = new TableCell();
            cel.ColumnSpan = 4;
            Label lblPass = new Label();
            int a = Convert.ToInt32(_id) + 1;
            lblPass.Text = "Passenger " + a.ToString() + " : " + passengerType;
            lblPass.ID = "Passenger " + _id;
            cel.Controls.Add(lblPass);
            row1.Cells.Add(cel);
            #endregion
            this.Rows.Add(row1);

            #region Row 2 Header
            TableRow row2 = new TableRow();
            row2.Font.Bold = true;

            TableCell cel1 = new TableCell();
            cel1.Controls.Add(new LiteralControl("First Name"));
            row2.Cells.Add(cel1);

            TableCell cel2 = new TableCell();
            cel2.Controls.Add(new LiteralControl("Last Name"));
            row2.Cells.Add(cel2);

            TableCell cel3 = new TableCell();
            cel3.Controls.Add(new LiteralControl("Age"));
            row2.Cells.Add(cel3);

            TableCell cel4 = new TableCell();
            cel4.Controls.Add(new LiteralControl("Gender"));
            row2.Cells.Add(cel4);
            #endregion
            this.Rows.Add(row2);

            #region Row 3 Input
            TableRow row3 = new TableRow();

            TableCell cel31 = new TableCell();
            TextBox tbfirsname = new TextBox();
            tbfirsname.ID = "tbfirsname" + _id;
            cel31.Controls.Add(tbfirsname);
            RequiredFieldValidator validfirstname = new RequiredFieldValidator();
            validfirstname.Text = "*";
            validfirstname.ControlToValidate = tbfirsname.ID;
            cel31.Controls.Add(validfirstname);
            row3.Cells.Add(cel31);

            TableCell cel32 = new TableCell();
            TextBox tblastname = new TextBox();
            tblastname.ID = "tblastname" + _id;
            cel32.Controls.Add(tblastname);
            RequiredFieldValidator validllastname = new RequiredFieldValidator();
            validllastname.Text = "*";
            validllastname.ControlToValidate = tblastname.ID;
            cel32.Controls.Add(validllastname);
            row3.Cells.Add(cel32);

            TableCell cel33 = new TableCell();
            DropDownList ddlAge = new DropDownList();
            ddlAge.ID = "ddlAge" + _id;
            #region age list
            if (passengerType == "Adult")
            {
                for (int i = 12; i < 65; i++)
                {
                    ListItem item = new ListItem(i.ToString());
                    ddlAge.Items.Add(item);
                }
            }
            else if (passengerType == "Children")
            {
                for (int i = 1; i < 12; i++)
                {
                    ListItem item = new ListItem(i.ToString());
                    ddlAge.Items.Add(item);
                }
            }
            else if (passengerType == "Senior")
            {
                for (int i = 65; i < 100; i++)
                {
                    ListItem item = new ListItem(i.ToString());
                    ddlAge.Items.Add(item);
                }
            }
            #endregion
            cel33.Controls.Add(ddlAge);
            row3.Cells.Add(cel33);

            TableCell cel34 = new TableCell();
            RadioButtonList rblGender = new RadioButtonList();
            rblGender.ID = "rblGender" + _id;
            #region Gender
            ListItem item1 = new ListItem("Male", "true");
            ListItem item2 = new ListItem("Female", "false");
            rblGender.Items.Add(item1);
            rblGender.Items.Add(item2);
            rblGender.SelectedIndex = 0;
            #endregion
            cel34.Controls.Add(rblGender);
            row3.Cells.Add(cel34);
            #endregion
            this.Rows.Add(row3);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int numofPass = Convert.ToInt32(Session["Adult"]) + Convert.ToInt32(Session["Child"]) + Convert.ToInt32(Session["Senior"]);
        List<Ticket> DepartTicketList = (List<Ticket>)Session["DepartTicketList"];

        for (int i = 0; i < numofPass; i++)
        {
            TextBox tbfirstname = (TextBox)FindControl(PlaceHolder1,"tbfirsname" + i.ToString());
            TextBox tblastname = (TextBox)FindControl(PlaceHolder1, "tblastname" + i.ToString());
            DropDownList ddlAge = (DropDownList)FindControl(PlaceHolder1, "ddlAge" + i.ToString());
            RadioButtonList rblGender = (RadioButtonList)FindControl(PlaceHolder1, "rblGender" + i.ToString());

            Passenger pass = new Passenger();
            pass.FirstName = tbfirstname.Text;
            pass.LastName = tblastname.Text;
            pass.IsMale = Convert.ToBoolean(rblGender.SelectedValue);
            pass.Age = Convert.ToInt32(ddlAge.SelectedValue);

            foreach (Ticket ticket in DepartTicketList)
            {
                ticket.TicketDetail[i].Passenger = pass;
            }
        }
        Session["DepartTicketList"] = DepartTicketList;
        if (Session["ReturnTicketList"] != null)
        {
            List<Ticket> ReturnTicketList = (List<Ticket>)Session["ReturnTicketList"];
            foreach (Ticket ticket in ReturnTicketList)
            {
                for (int i = 0; i < ticket.TicketDetail.Count; i++)
                {
                    ticket.TicketDetail[i].Passenger = DepartTicketList[0].TicketDetail[i].Passenger;
                }
            }
            Session["ReturnTicketList"] = ReturnTicketList;
        }
        Response.Redirect("~/Users/CheckBill.aspx");
    }

    private Control FindControl(Control parent, string id)
    {
        if (parent.FindControl(id) != null)
        {
            return parent.FindControl(id);
        }
        else
        {
            if (parent.Controls.Count > 0)
            {
                Control x = null;
                foreach (Control c in parent.Controls)
                {
                    x = FindControl(c, id);
                    if (x != null)
                    {
                        break;
                    }
                }
                return x;
            }
            else
            {
                return null;
            }
        }
    }
}
